package com.yunwei.dao;

import android.test.ActivityInstrumentationTestCase2;

import com.yunwei.BaseApplication;
import com.yunwei.activity.NoteActivity;

import java.util.Date;
import java.util.List;

/**
 * @author weijianxing
 */
public class NoteDaoTest extends ActivityInstrumentationTestCase2<NoteActivity> {


    private NoteActivity noteActivity;

    private NoteDao noteDao;

    public NoteDaoTest() {
        super(NoteActivity.class);
    }


    @Override
    protected void setUp() throws Exception {
        super.setUp();

        noteActivity = getActivity();
        noteDao = BaseApplication.getNoteDao(noteActivity);

    }


    public void testNoteDaoInsert() {

        Note note = new Note();
        note.setComment("comment");
        note.setDate(new Date());
        note.setText("text");

        /**
         * 插入
         */
        noteDao.insert(note);

        List<Note> noteList = noteDao.loadAll();

        assertEquals(note.getComment(), noteList.get(0).getComment());

        //删除所有数据
        noteDao.deleteAll();
    }


    public void testNoteDaoLoad() {
        Note note = getNote();

        noteDao.insert(note);

        /**
         * load 并不是每次都去数据库做查询,如果对象存在缓存中,那么直接在缓存中返回对象
         */
        note = noteDao.load(note.getId());


        /**
         * 刷新对象在缓存中保存的状态
         */
        noteDao.refresh(note);

    }


    public void testNoteDaoUpdate() {
        Note note = getNote();
        noteDao.insert(note);

        /**
         * 数据插入到数据库后,自动赋值id主键
         */
        assertNotNull(note.getId());

        note.setText("test new");

        /**
         * 更新
         */
        noteDao.update(note);


        /**
         * 根据id删除
         */
        noteDao.deleteByKey(note.getId());
    }

    private Note getNote() {
        Note note = new Note();
        note.setText("text");
        note.setComment("comment");
        note.setDate(new Date());
        return note;
    }

}
